package com.baidu.shop.service.impl;
import com.baidu.shop.entity.BrandEntity;
import com.baidu.shop.entity.SpecGroupEntity;
import com.baidu.shop.entity.SpuEntity;
import com.baidu.shop.mapper.BrandMapper;
import com.baidu.shop.mapper.CategoryMapper;
import com.baidu.shop.base.BaseApiService;
import com.baidu.shop.base.Result;
import com.baidu.shop.entity.CategoryEntity;
import com.baidu.shop.mapper.SpecificationMapper;
import com.baidu.shop.mapper.SpuMapper;
import com.baidu.shop.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @ClassName: CategoryServiceImpl
 * @Author: User
 * @date: 2020.08.27 19:58
 * @version: 1.0
 */
@RestController
@Transactional(readOnly = false)
public class CategoryServiceImpl extends BaseApiService implements CategoryService{
    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private BrandMapper bmapper;

    @Resource
    private SpecificationMapper mapper;

    @Resource
    private SpuMapper spuMapper;

    @Override
    public Result<List<CategoryEntity>> getCategoryByIdList(String categoryIds) {
        List<Integer> categoryIntIds = Arrays.asList(categoryIds.split(",")).stream().map(categoryId -> {
            return Integer.parseInt(categoryId);
        }).collect(Collectors.toList());
        List<CategoryEntity> categoryEntities = categoryMapper.selectByIdList(categoryIntIds);
        return this.setResultSuccess(categoryEntities);
    }

    @Override
    public Result<List<CategoryEntity>> deleteCategory(Integer id) {
        String msg="";
        String msgName="";
        CategoryEntity categoryEntity = categoryMapper.selectByPrimaryKey(id);
        //是否存在
        if (null == categoryEntity) return this.setResultError("删除的商品分类不存在");
        //是否是父节点
        if(categoryEntity.getIsParent()==1) return this.setResultError("该节点为父节点");
        //是否被品牌绑定
        List<BrandEntity> brandEntities = bmapper.selectByCBId(id);
        if (!brandEntities.isEmpty()) msg+="分类被["+brandEntities.get(0).getName()+"]....等、"+brandEntities.size()+"个品牌绑定,无法被删除!!!、、、";

        //是否被分组绑定
        Example specGroupExample = new Example(SpecGroupEntity.class);
        specGroupExample.createCriteria().andEqualTo("cid",id);
        List<SpecGroupEntity> specGroupEntities = mapper.selectByExample(specGroupExample);
        if (!specGroupEntities.isEmpty()){
            for (SpecGroupEntity specGroupEntity : specGroupEntities) { msgName+="["+specGroupEntity.getName()+"]"; }
            msg+="分类被旗下分组"+msgName+"绑定,无法被删除!!!、、、";
        }
        //是否被商品绑定
        Example spuExample = new Example(SpuEntity.class);
        spuExample.createCriteria().andEqualTo("cid3",id);
        List<SpuEntity> spuEntities = spuMapper.selectByExample(spuExample);
        if (!spuEntities.isEmpty()){
            msg+="分类被["+spuEntities.get(0).getTitle()+"]....等、"+spuEntities.size()+"个商品绑定,无法被删除!!!";
        }

        //判断错误提示长度是否为0
        if (msg.trim().length()>0) return this.setResultError(msg);
        //判断父类型下是否还有类型,无，设置父节点为字节点
        Example example = new Example(CategoryEntity.class);
        example.createCriteria().andEqualTo("parentId",categoryEntity.getParentId());
        if (categoryMapper.selectByExample(example).size()==1){
            CategoryEntity category = new CategoryEntity();
            category.setId(categoryEntity.getParentId());
            category.setIsParent(0);
            categoryMapper.updateByPrimaryKeySelective(category);
        }
        //删除类型
        categoryMapper.deleteByPrimaryKey(id);
        return this.setResultSuccess();
    }

    @Override
    public Result<List<CategoryEntity>> editCategory(CategoryEntity entity) {
        System.out.println(entity);
        int i = categoryMapper.updateByPrimaryKeySelective(entity);
        return this.setResultSuccess();
    }

    @Override
    public Result<List<CategoryEntity>> saveCategory(CategoryEntity entity) {
        CategoryEntity category = new CategoryEntity();
        category.setId(entity.getParentId());
        category.setIsParent(1);
        categoryMapper.updateByPrimaryKeySelective(category);

        int i = categoryMapper.insertSelective(entity);
        return this.setResultSuccess();
    }

    @Override
    @Transactional(readOnly = true)
    public Result<List<CategoryEntity>> getCategoryByPid(Integer pid) {

        CategoryEntity categoryEntity = new CategoryEntity();

        categoryEntity.setParentId(pid);

        List<CategoryEntity> list = categoryMapper.select(categoryEntity);

        return this.setResultSuccess(list);
    }
}
